package www.wen.com.common.lib.helper.loading

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.dylanc.loadinghelper.LoadingHelper
import com.gyf.immersionbar.ImmersionBar
import www.wen.com.common.R
import www.wen.com.common.lib.app.BaseApplication.Companion.CONTEXT
import www.wen.com.common.lib.extension.setOnClickListenerAgency

/**
 * @author WEN
 * @Description:
 * @date 2020/12/8 15:27
 */
class TitleBarUniversalAdapter(
    private var activity: Context,
    private var tilte: String = "",
    private var background:Int = ContextCompat.getColor(CONTEXT, R.color.color0F0F11),
    @DrawableRes private var resIdLeft: Int = R.drawable.ic_arrow_return_white,
    @DrawableRes private var resIdRight: Int = R.drawable.blue_ok,
    internal var action: ((view: View) -> Unit)? = null,
    private var isShowLeftText: Boolean = false,
    private var leftText: String = "",
    private var isShowRightImage: Boolean = false,
    private var isShowRightText: Boolean = false,
    private var rightText: String = "",
    private var rightTextSize: Float = 12f,
    private var rightTextColor: Int = ContextCompat.getColor(CONTEXT, R.color.colorFFFFFF),
    private var isShowSubtitle: Boolean = false,
    private var subtitle: String = "",
    private var subtitleColor: Int = ContextCompat.getColor(CONTEXT, R.color.color888888)
) : LoadingHelper.Adapter<TitleBarUniversalAdapter.ViewHolder>(),
        View.OnClickListener {

    lateinit var mViewHolder: ViewHolder

    override fun onBindViewHolder(holder: ViewHolder) {

        mViewHolder = holder

        holder.view.setBackgroundColor(background)

        holder.titleView.text = tilte
        holder.ivLeft.setImageResource(resIdLeft)

        if(isShowSubtitle){
            holder.tvSubtitle.text = subtitle
            holder.tvSubtitle.setTextColor(subtitleColor)
        }
        holder.tvSubtitle.isVisible = isShowSubtitle

        if (isShowRightText) {
            holder.tvRight.text = rightText
            holder.tvRight.textSize = rightTextSize
            holder.tvRight.setTextColor(rightTextColor)
            holder.tvRight.isVisible = true
        }

        if (isShowRightImage) {
            holder.ivRight.isVisible = true
            holder.ivRight.setImageResource(resIdRight)
        } else {
            holder.ivRight.isVisible = false
        }

        if(isShowLeftText){
            holder.ivLeft.isVisible = false
            holder.tvLeft.isVisible = true
            holder.tvLeft.text = leftText
        }else{
            holder.ivLeft.isVisible = true
            holder.tvLeft.isVisible = false
        }
        setOnClickListenerAgency(this, holder.ivLeft, holder.ivRight, holder.tvRight,holder.tvLeft)
        ImmersionBar.setTitleBar(activity as Activity, holder.placeholderView)
    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return ViewHolder(inflater.inflate(R.layout.layout_title_universal, parent, false))
    }

    class ViewHolder(val view: View) : LoadingHelper.ViewHolder(view) {

        var ivLeft: ImageView = view.findViewById(R.id.ivLeft)
        var tvLeft: TextView = view.findViewById(R.id.tvLeft)
        var placeholderView: View = view.findViewById(R.id.placeholderView)
        var titleView: TextView = view.findViewById(R.id.tvTitle)
        var ivRight: ImageView = view.findViewById(R.id.ivRight)
        var tvRight: TextView = view.findViewById(R.id.tvRight)
        var tvSubtitle: TextView = view.findViewById(R.id.tvSubtitle)

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.ivLeft -> {

            }
        }
        action?.invoke(v)
    }

}